package org.kleathome.dcpu16;

import org.kleathome.dcpu16.emulator.Emulator;

public class DCPU16Ife extends DCPU16Instruction
{
	private boolean wasSuccess;
	
	@Override
	public String parseInstruction(String in)
	{
		return super.parseInstruction(in.substring(4).trim());
	}

	@Override
	public int getCycleCost()
	{
		return 2 + super.getCycleCost() + (wasSuccess?0:1);
	}

	@Override
	public int getOpCode()
	{
		return 0x0C;
	}

	@Override
	public String getOpCodeLiteral()
	{
		return "IFE";
	}

	@Override
	public void execute()
	{
		char vA = valueA.getValue();
		char vB = valueB.getValue();
		
		if (vA == vB)
		{
			wasSuccess = true;
			char instruction = Emulator.nextInstruction();
			DCPU16Instruction op = DCPU16Instruction.getInstruction((char)(instruction & 0x0F));
			op.setA((char)((instruction >> 4)  & 0x3F));
			op.setB((char)((instruction >> 10)  & 0x3F));
			System.out.println(op);
			op.execute();
		}
		else
		{
			wasSuccess = false;
			char instruction = Emulator.nextInstruction();
			DCPU16Instruction op = DCPU16Instruction.getInstruction((char)(instruction & 0x0F));
			op.setA((char)((instruction >> 4)  & 0x3F));
			op.setB((char)((instruction >> 10)  & 0x3F));
		}
	}

	@Override
	public DCPU16Instruction getNewObject()
	{
		return new DCPU16Ife();
	}
}
